package net.jielan188.hd.util.common.md5;

/**
 * Md5工具
 *@author huangguogang
 *@date 2017-01-23
 */

public class Md5Utils {

  /**
   * Md5加密
  */
  public static String getMD5( byte[] source ) {

    String s = null;
    char [] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
        '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    final int temp = 0xf;
    final int arraySize = 32;
    final int strLen = 16;
    final int offset = 4;
    try {
      java.security.MessageDigest md = java.security.MessageDigest
        .getInstance( "MD5" );
      md.update(source);
      byte [] tmp = md.digest();
      char [] str = new char[arraySize];

      int k = 0;
      for (int i = 0; i < strLen; i++) {
        byte byte0 = tmp[i];
        str[k++] = hexDigits[byte0 >>> offset & temp];
        str[k++] = hexDigits[byte0 & temp];
      }
      s = new String( str );
    } catch (Exception e) {
      e.printStackTrace();
    }
    return s;
  }
}